package com.ztf.room.controller;

import com.ztf.room.bean.RoomAdmin;
import com.ztf.room.bean.Student;
import com.ztf.room.bean.Teachers;
import com.ztf.room.service.AdminService;
import com.ztf.room.service.StudentService;
import com.ztf.room.service.TeacherService;
import com.ztf.room.utils.Result;
import com.ztf.room.utils.ResultInfo;
import com.ztf.room.utils.SessionConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Controller
public class CommonController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private AdminService adminService;

    @PostMapping("/adminLogin")
    @ResponseBody
    public Result adminLogin(String account, String password, HttpSession session){
        RoomAdmin login = adminService.login(account, password);
        session.setAttribute(SessionConstant.ADMIN_NAME,login);
        session.setAttribute("role","1");
        return ResultInfo.success();
    }

    @PostMapping("/teacherLogin")
    @ResponseBody
    public Result teacherLogin(String account,String password,HttpSession session){
        Teachers teachers = teacherService.login(account,password);
        session.setAttribute(SessionConstant.TEACHER_NAME,teachers);
        session.setAttribute("role","2");
        return ResultInfo.success();
    }
    @PostMapping("/studentLogin")
    @ResponseBody
    public Result studentLogin(String account,String password,HttpSession session){
        Student student = studentService.login(account,password);
        session.setAttribute(SessionConstant.STUDENT_NAME,student);
        session.setAttribute("role","3");
        return ResultInfo.success();
    }

    @RequestMapping("/login")
    public String login(){
        return "/login.html";
    }
    @RequestMapping("/")
    public String index(){
        return "/index.jsp";
    }
}
